package com.nf.health.app.mine.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.nf.health.app.Constants;
import com.nf.health.app.R;
import com.nf.health.app.activity.global.BaseActivity;
import com.nf.health.app.utils.Code;
import com.nf.health.app.utils.PreferenceHelper;
import com.nf.health.app.utils.Util;
import com.nf.health.app.utils.ViewUtil;

/**
 * @ClassName: ModifyAccountActivity
 * @Description: 修改用户页面
 * @author Ian
 * @date 2015-11-4 上午9:55:27
 *
 */
public class ModifyAccountActivity extends BaseActivity implements OnClickListener {
	private TextView tv_acc_pwd,tv_acc_phone,tv_acc_msg;
	private TextView tv_current_acc;
	private Button btn_get_code;
	private Button btn_commit;
	private EditText et_acc_pw;
	private EditText et_acc_phone;
	private EditText et_verification_code;
	private TimeCount timeCount;
	String oldpwd;
	String newPhone;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleBar(R.layout.titlebar_base);
		setContent(R.layout.activity_set_account);
		ViewUtil.setText(this, R.id.titlebar_title_tv, "修改账号");
		tv_current_acc = ViewUtil.getView(this, R.id.tv_current_acc);
		tv_acc_pwd=(TextView)findViewById(R.id.tv_acc_pwd);
		tv_acc_phone=(TextView)findViewById(R.id.tv_acc_phone);
		tv_acc_msg=(TextView)findViewById(R.id.tv_acc_msg);

		et_acc_pw = ViewUtil.getView(this, R.id.et_acc_pw);
		et_acc_phone = ViewUtil.getView(this, R.id.et_acc_phone);
		et_verification_code = ViewUtil.getView(this, R.id.et_verification_code);


		btn_get_code = ViewUtil.getView(this, R.id.btn_get_code);
		btn_commit = ViewUtil.getView(this, R.id.btn_commit);
		initData();
		initListener();

		timeCount = new TimeCount(60000, 1000);
	}

	private void initData() {
		resetTVColor(tv_acc_pwd);
		resetTVColor(tv_acc_phone);
		resetTVColor(tv_acc_msg);
		tv_current_acc.setText(PreferenceHelper.getString(PreferenceHelper.USERNAME, ""));
	}
	private void initListener() {
		btn_get_code.setOnClickListener(this);
		btn_commit.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn_get_code:
				String phone = et_acc_phone.getText().toString();
				if (TextUtils.isEmpty(phone)) {
					showToast(R.string.phone_not_blank);
					break;
				}
				if (!Util.isPhone(phone)) {
					showToast(R.string.phone_error_prompt);
					break;
				}
				timeCount.start();
				Constants.LOCAL_CODE = Code.getInstance().getCode();
//			if(LogUtil.IS_DEBUG){
//				et_verification_code.setText(Constants.LOCAL_CODE);
//			}
				provider.requestMSGCode(phone,"verifycode","verifycode");
				break;
			case R.id.btn_commit:
				oldpwd = et_acc_pw.getText().toString();
				newPhone = et_acc_phone.getText().toString();
				String smsCode = et_verification_code.getText().toString();
				if(TextUtils.isEmpty(oldpwd)){
					showToast("原密码不能为空！");
					break;
				}
				if(TextUtils.isEmpty(newPhone)){
					showToast(R.string.phone_not_blank);
					break;
				}
				if (!Util.isPhone(newPhone)) {
					showToast(R.string.phone_error_prompt);
					break;
				}
				if(TextUtils.isEmpty(oldpwd)){
					showToast(R.string.pwd_not_blank);
					break;
				}
				if(!Util.isPassword(oldpwd)){
					showToast(R.string.pwd_style_prompt);
					break;
				}
				if(TextUtils.isEmpty(smsCode)){
					showToast(R.string.auth_code_error_prompt);
					break;
				}

				provider.requestMSGCodeCode(newPhone,smsCode, "verifycode", "yanzheng");

				break;
		}
		super.onClick(v);
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("verifycode")) {
			hideLoading();
			showToast("验证码发送成功！");
		}else if(actionName.equals("modifyAccount")){
			hideLoading();
			showToast("修改成功");
			PreferenceHelper.putString(PreferenceHelper.USERNAME, et_acc_phone.getText().toString());
			finish();
		}else if ("yanzheng".equals(actionName)) {//判断验证码与手机号是否相符

		String msg = object+"";
		if(msg.contains("验证成功")){
			//TODO 修改账户接口
			provider.requestModifyAccount(newPhone, Util.encode(oldpwd), "modifyAccount");
		}else{
			showToast("验证码错误");
		}
	}
	}

	public void resetTVColor(TextView tv) {
		String str = tv.getText().toString();
		int start = 0;
		int end = 1;
		SpannableStringBuilder builder = new SpannableStringBuilder(str);
		builder.setSpan(new ForegroundColorSpan(Color.RED), start, end,
				Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
		tv.setText(builder);
	}

	//计时重发
	private class TimeCount extends CountDownTimer {
		/**
		 * @param millisInFuture 计时器执行的时长
		 * @param countDownInterval 执行一次的时间间隔
		 */
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}
		@Override
		public void onTick(long millisUntilFinished) {
			btn_get_code.setText("重发（"+millisUntilFinished / 1000+"秒）");
//	        	et_verification_code.setFocusable(true);
//	        	et_verification_code.requestFocus();
			btn_get_code.setClickable(false);
		}

		@Override
		public void onFinish() {
			resetCodeBtn();
		}

	}
	private void resetCodeBtn() {
		btn_get_code.setClickable(true);
		btn_get_code.setText("获取验证码");
	}
}
